package main;


import com.kcbsdk.AFetchType;
import com.kcbsdk.MyManage.AHeadPortrait;
import com.kcbsdk.MyRankBa.ACommentList;
import com.kcbsdk.MyRankBa.AObbligStyle;
import com.kcbsdk.MyRankBa.AObbligDetail;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.MyRankBa.ARankTopList;
import com.kcbsdk.CSvrRankBa;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyRankBa.ASongGiftList;
import com.kcbsdk.MyRankBa.ASongDetail;
import com.kcbsdk.MyRankBa.ASongList;
import com.kcbsdk.MyRankBa.AUserExtrList;
import com.kcbsdk.MyRankBa.AUserDetail;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.cjmkcs;
import com.kcbsdk.wpapper;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/4/5.
 */

public class MyRankBa implements wpapper {
    public CSvrRankBa mSvrRankBa;
    public Activity mActivity = null;
    public EventHandler mEventHandler = null;

    public byte gCloudType;
    public int g4DownAudioCounter;
    public int g4DownAudioLength;
    public int g4DownVideoCounter;
    public int g4DownVideoLength;

    public ARankTopList[] mARankTopList;
    public int g4RankTopListMax;

    public ASongList[] mASongList;
    public int g4SongListMax;

    public ASongGiftList[] mASongGiftList;
    public int g4SongGiftListMax;

    public ACommentList[] mACommentList;
    public int g4CommentListMax;
    public AObbligatoList[] mAObbligatoList;
    public int g4ObbligatoListMax;


    public AUserDetail mAUserDetail;
    public ASongDetail mASongDetail;
    public AObbligDetail mAObbligDetail;




    public byte gSaveObbligStyle[];
    public byte gSaveUserExtr[];
    public int g2SaveAreaData[];								// 0=当前areaData 1=最新readType 2=最新areaData
    public int g4SaveUserInfo[];								// 0=当前userID 1=最新userID 2=最新头像
    public int g4SaveSongInfo[];								// 0=当前userID 1=最新userID 2=最新songID
    public int g4SaveObbligaInfo[];							// 0=当前musicID0 1=最新musicID0 2=最新musicID1
    public int g4SaveSongListDbs[];							// 0=当前userID 1=最新readType 2=最新userID
    public int g4SaveCommentListDbs[];						// 0=当前userID 1=当前songID 2=最新readType 3=最新userID 4=最新songID
    public int g4SaveSongGiftLisDbs[];						// 0=当前userID 1=当前songID 2=最新readType 3=最新userID 4=最新songID



    public MyRankBa(){
        mSvrRankBa = new CSvrRankBa();

        gSaveObbligStyle = new byte[2];
        gSaveUserExtr = new byte[2];
        g2SaveAreaData = new int[3];								// 0=当前areaData 1=最新readType 2=最新areaData
        g4SaveUserInfo = new int[3];								// 0=当前userID 1=最新userID 2=最新头像
        g4SaveSongInfo = new int[3];								// 0=当前userID 1=最新userID 2=最新songID
        g4SaveObbligaInfo = new int[3];							// 0=当前musicID0 1=最新musicID0 2=最新musicID1
        g4SaveSongListDbs = new int[3];							// 0=当前userID 1=最新readType 2=最新userID
        g4SaveCommentListDbs = new int[5];						// 0=当前userID 1=当前songID 2=最新readType 3=最新userID 4=最新songID
        g4SaveSongGiftLisDbs = new int[5];						// 0=当前userID 1=当前songID 2=最新readType 3=最新userID 4=最新songID
    }

    public void pollingTimer(){
        int methodType;
        for (int counter = 0; counter < 8; counter++){
            methodType = mSvrRankBa.getMethodType();
            if (methodType > 0){
                String message = mSvrRankBa.getMethodMessage(methodType);
                boolean isFail = ("ok".equals(message)) ? false : true;
                Log("mMyRankBa.getMethodType " + String.format("%08X %s", methodType, message));
                if (!isFail){
                    if ((methodType & 0x20000000) > 0) {
                        int retMax[] = new int[1];
                        switch (methodType & 0x000000ff) {
                            case TOOLS_DBS_LIST_RANK_TOP:
                                mARankTopList = mSvrRankBa.methodRankTopDbs(methodType, g2SaveAreaData[0], retMax);
                                g4RankTopListMax = retMax[0];
                                g2SaveAreaData[0] = 0;
                                if (g2SaveAreaData[1] > 0) {                // 还有需要继续做的
                                    mSvrRankBa.fetchRankTopList(gCloudType, g2SaveAreaData[1] & 0x03, g2SaveAreaData[2]);
                                    g2SaveAreaData[0] = g2SaveAreaData[2];
                                    g2SaveAreaData[1] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_CLOUD:
                                mASongList = mSvrRankBa.methodSongListDbs(methodType, g4SaveSongListDbs[0], retMax);
                                g4SongListMax = retMax[0];
                                g4SaveSongListDbs[0] = 0;                // 完成
                                if (g4SaveSongListDbs[1] > 0) {            // 还有需要继续做的
                                    mSvrRankBa.fetchSongList(gCloudType, g4SaveSongListDbs[1] & 0x03, g4SaveSongListDbs[2]);
                                    g4SaveSongListDbs[0] = g4SaveSongListDbs[2];
                                    g4SaveSongListDbs[1] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_COMMENT:
                                mACommentList = mSvrRankBa.methodCommentListDbs(methodType, g4SaveCommentListDbs[0], g4SaveCommentListDbs[1], retMax);
                                g4CommentListMax = retMax[0];
                                g4SaveCommentListDbs[0] = 0;                // 完成
                                if (g4SaveCommentListDbs[2] > 0) {            // 还有需要继续做的
                                    mSvrRankBa.fetchCommentList(gCloudType, g4SaveSongListDbs[2] & 0x03, g4SaveSongListDbs[3], g4SaveSongListDbs[4]);
                                    g4SaveCommentListDbs[0] = g4SaveCommentListDbs[2];
                                    g4SaveCommentListDbs[1] = g4SaveCommentListDbs[3];
                                    g4SaveCommentListDbs[2] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_SONG_GIFT:
                                mASongGiftList = mSvrRankBa.methodSongGiftListDbs(methodType, g4SaveSongGiftLisDbs[0], g4SaveSongGiftLisDbs[1], retMax);
                                g4SongGiftListMax = retMax[0];
                                g4SaveSongGiftLisDbs[0] = 0;                // 完成
                                if (g4SaveSongGiftLisDbs[2] > 0) {            // 还有需要继续做的
                                    mSvrRankBa.fetchSongGiftList(gCloudType, g4SaveSongGiftLisDbs[2] & 0x03, g4SaveSongGiftLisDbs[3], g4SaveSongGiftLisDbs[4]);
                                    g4SaveSongGiftLisDbs[0] = g4SaveSongGiftLisDbs[2];
                                    g4SaveSongGiftLisDbs[1] = g4SaveSongGiftLisDbs[3];
                                    g4SaveSongGiftLisDbs[2] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_OBBLIGATO:
                                mAObbligatoList = mSvrRankBa.methodObbligatoListDbs(methodType, gSaveObbligStyle[0] & 0x3f, retMax);
                                g4ObbligatoListMax = retMax[0];
                                gSaveObbligStyle[1] = gSaveObbligStyle[0];
                                gSaveObbligStyle[0] = 0;
                                break;
                            case TOOLS_DBS_LIST_USER_DETAIL:
                                mAUserDetail = mSvrRankBa.methodFetchUserDetail(methodType & 0xffff);
                                g4SaveUserInfo[0] = 0;
                                if (g4SaveUserInfo[2] > 0) {                    // 还有需要继续做的
                                    mSvrRankBa.fetchUserDetail(gCloudType, g4SaveUserInfo[1], g4SaveUserInfo[2] & 0x0f);
                                    g4SaveUserInfo[2] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_SONG_DETAIL:
                                mASongDetail = mSvrRankBa.methodFetchSongDetail(methodType & 0xffff);
                                g4SaveSongInfo[0] = 0;
                                if (g4SaveSongInfo[2] > 0) {                    // 还有需要继续做的
                                    mSvrRankBa.fetchSongDetail(gCloudType, g4SaveSongInfo[1], g4SaveSongInfo[2]);
                                    g4SaveSongInfo[2] = 0;
                                }
                                break;
                            case TOOLS_DBS_LIST_OBB_DETAIL:
                                mAObbligDetail = mSvrRankBa.methodFetchObbligDetail(methodType & 0xffff);
                                g4SaveObbligaInfo[0] = 0;
                                if (g4SaveObbligaInfo[2] > 0) {                    // 还有需要继续做的
                                    mSvrRankBa.fetchObbligDetail(gCloudType, g4SaveObbligaInfo[1], g4SaveObbligaInfo[2]);
                                    g4SaveObbligaInfo[2] = 0;
                                }
                                break;
                        }
                    }
                    mEventHandler.onComplete(methodType, isFail, message);
                }
            }
            else {
                break;
            }
        }
        int newEvent = mSvrRankBa.pollingEvent();
        if (newEvent > 0){
//            Log("mMyRankBa.newEvent " + String.format("%08X", newEvent));
            int[] retValue = new int[2];
            if ((newEvent & RANKBA_EVENT_DOWN_AUDIO_FILE) > 0) {
                mSvrRankBa.eventDownloadAudio(retValue);
                g4DownAudioCounter = retValue[0];
                g4DownAudioLength = retValue[1];
            }
            if ((newEvent & RANKBA_EVENT_DOWN_VIDEO_FILE) > 0) {
                mSvrRankBa.eventDownloadVideo(retValue);
                g4DownVideoCounter = retValue[0];
                g4DownVideoLength = retValue[1];
            }
            if ((newEvent & RANKBA_EVENT_NEW_IMG_SMALL) > 0){
                mSvrRankBa.eventDownImgSmall();
            }
            if ((newEvent & RANKBA_EVENT_NEW_IMG_MIDDLE) > 0){
                mSvrRankBa.eventDownImgMiddle();
            }
            if ((newEvent & RANKBA_EVENT_NEW_IMG_LARGE) > 0){
                mSvrRankBa.eventDownImgLarge();
            }
            if ((newEvent & RANKBA_EVENT_NEW_IMG_RANK) > 0){
                mSvrRankBa.eventDownImgRank();
            }
            mEventHandler.onComplete(0x00000000, false, "ok");
        }
    }
    public void Log(String text){
        Log.d("MSYS", text);
    }
    public void Toast(String text){
        Toast.makeText(mActivity, text, Toast.LENGTH_LONG).show();
        Log.d("MSYS", text);
    }
    public void  setUiEventCallBack(Activity activity, EventHandler handler){
        mActivity = activity;
        mSvrRankBa.setMethodType();
        mEventHandler = handler;
        MREG.g4LastSelect = tRANKBA;
//        MSTRING(String.format("mMyRankBa setUiEventCallBack"));
    }
    public void selectCloudType(ACloudType aCloudType){
        gCloudType = (byte)aCloudType.ordinal();
    }

    // 获取榜列表方法 //////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchRankTopDbs(AFetchType aFetchType, int areaData){

        if (g2SaveAreaData[0] == 0){							// 上次已经完成
            g2SaveAreaData[0] = areaData;
            mSvrRankBa.fetchRankTopList(gCloudType, aFetchType.ordinal(), areaData);
        }
        else {													// 上次还没有完成
            g2SaveAreaData[1] = 0x80 | aFetchType.ordinal();			// 最新readType
            g2SaveAreaData[2] = areaData;						// 最新areaData
        }
    }
    // 成功返回获取榜列表
    public boolean isSucessTopListDbs(int methodType)	{
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_RANK_TOP){
            return true;
        }
        return false;
    }
    // 获取歌曲列表方法 //////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchSongListDbs(AFetchType aFetchType, int userID){
        if (g4SaveSongListDbs[0] == 0){							// 上次已经完成
            g4SaveSongListDbs[0] = userID;
            mSvrRankBa.fetchSongList(gCloudType, (int)aFetchType.ordinal(), userID);
        }
        else {													// 上次还没有完成
            g4SaveSongListDbs[1] = 0x80 | (int)aFetchType.ordinal();		// 最新readType
            g4SaveSongListDbs[2] = userID;						// 保存最新userID
        }
    }
    // 成功返回歌曲列表
    public boolean isSucessSongListDbs(int methodType)	{
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_SONG_GIFT){
            return true;
        }
        return false;
    }
    // 获取评论列表方法 //////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchCommentListDbs(AFetchType aFetchType, int userID, int songID){
        if (g4SaveCommentListDbs[0] == 0){						// 上次已经完成
            g4SaveCommentListDbs[0] = userID;
            g4SaveCommentListDbs[1] = songID;
            mSvrRankBa.fetchCommentList(gCloudType, (int)aFetchType.ordinal(), userID, songID);
        }
        else {													// 上次还没有完成
            g4SaveCommentListDbs[2] = 0x80 | (int)aFetchType.ordinal();	// 最新readType
            g4SaveCommentListDbs[3] = userID;					// 保存最新userID
            g4SaveCommentListDbs[4] = songID;					// 保存最新songID
        }
    }
    // 成功返回评论列表
    public boolean isSucessCommentListDbs(int methodType)	{
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_COMMENT){
            return true;
        }
        return false;
    }
    // 获取歌曲礼物列表方法 ////////////////////////////////////////////////////////////////////////////////////////
    public void fetchSongGiftListDbs(AFetchType aFetchType, int userID, int songID){
        if (g4SaveSongGiftLisDbs[0] == 0){						// 上次已经完成
            g4SaveSongGiftLisDbs[0] = userID;
            g4SaveSongGiftLisDbs[1] = songID;
            mSvrRankBa.fetchSongGiftList(gCloudType, (int)aFetchType.ordinal(), userID, songID);
        }
        else {													// 上次还没有完成
            g4SaveSongGiftLisDbs[2] = 0x80 | (int)aFetchType.ordinal();	// 最新readType
            g4SaveSongGiftLisDbs[3] = userID;					// 保存最新userID
            g4SaveSongGiftLisDbs[4] = songID;					// 保存最新songID
        }
    }
    // 成功返回歌曲礼物列表
    public boolean isSucessSongGiftListDbs(int methodType){
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_SONG_GIFT){
            return true;
        }
        return false;
    }
    // 获取伴奏列表方法 ///////////////////////////////////////////////////////////////////////////////////////////
    public void fetchObbligatoListDbs(AFetchType aFetchType, AObbligStyle aObbligStyle, String name){		// 歌曲伴奏
        if (gSaveObbligStyle[0] == 0){
            gSaveObbligStyle[0] = (byte)(0x40 | aObbligStyle.ordinal());
            mSvrRankBa.fetchObbligatoList(gCloudType, (int)aFetchType.ordinal(), aObbligStyle.ordinal(), name);
        }
    }
    // 成功返回伴奏列表
    public boolean isSucessObbligatoListDbs(int methodType, AObbligStyle aObbligStyle){
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_OBBLIGATO){
            if ((gSaveObbligStyle[1] & 0x3f) == (int)aObbligStyle.ordinal()){
                return true;
            }
        }
        return false;
    }

    // 获取用户扩展信息列表方法，包括用户图片、关注的人、粉丝、收藏、未读消息 //////////////////////////////////////
    public void fetchUserExtrListDbs(AFetchType aFetchType, AUserExtrList aUserExtrList, int userID){
        if (gSaveUserExtr[0] == 0){
            gSaveUserExtr[0] =  (byte)(0x40 | aUserExtrList.ordinal());
            mSvrRankBa.fetchUserExtrList(gCloudType, (int)aFetchType.ordinal(), gSaveUserExtr[0], userID);
        }
    }
    // 收到伴奏列表事件
/*public boolean isEventUserExtrListDbs(AObbligStyle aObbligStyle){
	if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_OBBLIGATO_LIST) > 0){
		if (gSaveObbligStyle[1] == ((char)aObbligStyle + 1)){
			mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_OBBLIGATO_LIST;
			return true;
		}
	}
	return false;
}
*/
// 获取用户信息方法 ///////////////////////////////////////////////////////////////////////////////////////////
    public void fetchUserDetail(int userID, AHeadPortrait aHeadPortrait){
        if (g4SaveUserInfo[0] == 0){							// 上次已经完成
            g4SaveUserInfo[0] = userID;
            mSvrRankBa.fetchUserDetail(gCloudType, userID, (int)aHeadPortrait.ordinal());
        }
        else {													// 上次还没有完成
            g4SaveUserInfo[1] = userID;							// 保存最新userID
            g4SaveUserInfo[2] = 0x80 | (int)aHeadPortrait.ordinal();		// 最新头像
        }
    }
    // 获取用户信息成功返回
    public boolean isSucessFetchUserDetail(int methodType){
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_USER_DETAIL){
            return true;
        }
        return false;
    }
    // 获取歌曲信息方法 ///////////////////////////////////////////////////////////////////////////////////////////
    public void fetchSongDetail(int userID, int songID){
        if (g4SaveSongInfo[0] == 0){							// 上次已经完成
            g4SaveSongInfo[0] = userID;
            mSvrRankBa.fetchSongDetail(gCloudType, userID, songID);
        }
        else {													// 上次还没有完成
            g4SaveSongInfo[1] = userID;							// 保存最新userID
            g4SaveSongInfo[2] = songID;							// 最新songID
        }
    }
    // 获取歌曲信息成功返回
    public boolean isSucessFetchSongDetail(int methodType){
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_SONG_DETAIL){
            return true;
        }
        return false;
    }
    // 获取伴奏信息方法 ///////////////////////////////////////////////////////////////////////////////////////////
    public void fetchObbligaDetail(int musicID0, int musicID1){
        if (g4SaveObbligaInfo[0] == 0){							// 上次已经完成
            g4SaveObbligaInfo[0] = musicID0;
            mSvrRankBa.fetchObbligDetail(gCloudType, musicID0, musicID1);
        }
        else {													// 上次还没有完成
            g4SaveObbligaInfo[1] = musicID0;						// 保存最新musicID0
            g4SaveObbligaInfo[2] = musicID1;						// 最新musicID1
        }
    }
    // 获取伴奏信息成功返回
    public boolean isSucessFetchObbligaDetail(int methodType){
        if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_OBB_DETAIL){
            return true;
        }
        return false;
    }


    // 获取歌曲文件方法 ///////////////////////////////////////////////////////////////////////////////////////////
    public void fetchSongFile(int userID, int songID, boolean video){
        mSvrRankBa.fetchSongFile(gCloudType, userID, songID, video);
    }
    // 收到音频文件下载事件
    public boolean isEventDownloadAudio()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_DOWN_AUDIO_FILE) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_DOWN_AUDIO_FILE;
            return true;
        }
        return false;
    }
    // 收到视频文件下载事件
    public boolean isEventDownloadVideo()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_DOWN_VIDEO_FILE) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_DOWN_VIDEO_FILE;
            return true;
        }
        return false;
    }
// 获取歌曲文件方法 ///////////////////////////////////////////////////////////////////////////////////////////


    // 收到小头像文件下载事件
    public boolean isEventDownImgSmall()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_NEW_IMG_SMALL) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_NEW_IMG_SMALL;
            return true;
        }
        return false;
    }
    // 收到中头像文件下载事件
    public boolean isEventDownImgMiddle()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_NEW_IMG_MIDDLE) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_NEW_IMG_MIDDLE;
            return true;
        }
        return false;
    }
    // 收到大头像文件下载事件 /data/user/0/com.hsav.kcxb/files/KCXB/photo/f21_1_0.jpg
    public boolean isEventDownImgLarge()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_NEW_IMG_LARGE) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_NEW_IMG_LARGE;
            return true;
        }
        return false;
    }
    // 收到榜头像文件下载事件
    public boolean isEventDownImgRank()	{
        if ((mSvrRankBa.g4EventMethod & RANKBA_EVENT_NEW_IMG_RANK) > 0){
            mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_NEW_IMG_RANK;
            return true;
        }
        return false;
    }


    public void MSTRING(String text){
        Log.d("MSYS", text);
    }

}
